/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import bean.*;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.commons.io.IOUtils;



/**
 *
 * @author wudihua
 */
public class MakeTempalteStringUtil {
        
    public static void main(String[] args) {
        PatientBean patientBean = new PatientBean();
        patientBean.setPatientId("001");
        patientBean.setIdCode("2.16.840.1.113883.2.23.1.9.1");
        patientBean.setIdNumber("110101200301120019");
        patientBean.setSocialCode("2.16.840.1.113883.2.23.1.9.2");
        patientBean.setSocialNumber("100000000000");
        patientBean.setPatientAllName("刘康");
        patientBean.setPatientFamilyName("刘");
        patientBean.setPatientGivenName("康");
        patientBean.setType("phone");
        patientBean.setValue("13800138000");
        patientBean.setUse("mobile");
        patientBean.setGender("male");
        patientBean.setBirthDate("2003-01-12");
        patientBean.setBirthTime("09:12:35");

        patientBean.setAddressUse("home");
        patientBean.setAddressText("北京市东城区景山前街4号");
        patientBean.setAddressLine("景山前街4号");
        patientBean.setAddressCity("北京市");
        patientBean.setAddressDistrict("东城区");
        patientBean.setAddressState("北京");
        patientBean.setAddressPostalCode("100010");

        patientBean.setContactRelationship("parent");
        patientBean.setContactName("刘勇");
        patientBean.setContactType("phone");
        patientBean.setContactValue("13012345678");
        patientBean.setContactGender("male");

        //String s = makePatientXml(patientBean);
        //System.out.println(s);
    }

    private static String getTemplateString(String path) {
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        StringBuilder sbstr =new StringBuilder();
        String data = null;
        try {
//            inputStreamReader = new InputStreamReader(
//                    new FileInputStream(System.getProperty("user.dir")+"/template/patientTemplate.xml"));
            
            inputStreamReader = new InputStreamReader(
                    new FileInputStream(System.getProperty("user.dir")+path));
            bufferedReader = new BufferedReader(inputStreamReader);
            while ((data = bufferedReader.readLine()) != null){
                sbstr.append(data);
            }
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
             ex.printStackTrace();
        } finally {
            IOUtils.closeQuietly(inputStreamReader);
            IOUtils.closeQuietly(bufferedReader);
        }
        return sbstr.toString();
    }
    //患者xml生成
    public static String makePatientString(PatientBean patientBean, String path) {
        
       
        String patientString = getTemplateString(path);
           
        patientString = patientString.replace("${patient.patientId}",patientBean.getPatientId());
        patientString = patientString.replace("${patient.idCode}",patientBean.getIdCode());
        patientString = patientString.replace("${patient.idNumber}",patientBean.getIdNumber());
        patientString = patientString.replace("${patient.socialCode}",patientBean.getSocialCode());
        patientString = patientString.replace("${patient.socialNumber}",patientBean.getSocialNumber());

        patientString = patientString.replace("${patient.patientAllName}", patientBean.getPatientAllName());
        patientString = patientString.replace("${patient.patientFamilyName}", patientBean.getPatientFamilyName());
        patientString = patientString.replace("${patient.patientGivenName}", patientBean.getPatientGivenName());


        patientString = patientString.replace("${patient.type}",patientBean.getType());
        patientString = patientString.replace("${patient.value}",patientBean.getValue());
        patientString = patientString.replace("${patient.use}",patientBean.getUse());
        patientString = patientString.replace("${patient.gender}",patientBean.getGender());
        patientString = patientString.replace("${patient.birthDate}",patientBean.getBirthDate());
        patientString = patientString.replace("${patient.birthTime}",patientBean.getBirthTime());

        patientString = patientString.replace("${patient.addressUse}",patientBean.getAddressUse());
        patientString = patientString.replace("${patient.addressText}",patientBean.getAddressText());
        patientString = patientString.replace("${patient.addressLine}",patientBean.getAddressLine());
        patientString = patientString.replace("${patient.addressCity}",patientBean.getAddressCity());
        patientString = patientString.replace("${patient.addressDistrict}",patientBean.getAddressDistrict());
        patientString = patientString.replace("${patient.addressState}",patientBean.getAddressState());
        patientString = patientString.replace("${patient.addressPostalCode}",patientBean.getAddressPostalCode());

        patientString = patientString.replace("${patient.contactRelationship}",patientBean.getContactRelationship());
        patientString = patientString.replace("${patient.contactName}",patientBean.getContactName());
        patientString = patientString.replace("${patient.contactType}",patientBean.getContactType());
        patientString = patientString.replace("${patient.contactValue}",patientBean.getContactValue());
        patientString = patientString.replace("${patient.contactGender}",patientBean.getContactGender());

       
        
        return patientString;
    }
    
    
    //就诊xml生成
    public static String makeEncounterString(EncounterBean encounterBean, String path) {
//        InputStreamReader inputStreamReader = null;
//        BufferedReader bufferedReader = null;
//        StringBuilder sbstr =new StringBuilder();
//        String encounterString = null;
//        try {
////            inputStreamReader = new InputStreamReader(
////                    new FileInputStream(System.getProperty("user.dir")+"/template/encounterTemplate.xml"));
//
//            inputStreamReader = new InputStreamReader(
//                    new FileInputStream(System.getProperty("user.dir")+path));
//            bufferedReader = new BufferedReader(inputStreamReader);
//            while ((encounterString = bufferedReader.readLine()) != null){
//                sbstr.append(encounterString);
//            }
            String encounterString = getTemplateString(path);
           
            encounterString = encounterString.replace("${encounter.encounterId}",encounterBean.getEncounterId());
            encounterString = encounterString.replace("${encounter.historyStatus}",encounterBean.getHistoryStatus());
            encounterString = encounterString.replace("${encounter.category}",encounterBean.getCategory());
            encounterString = encounterString.replace("${encounter.status}",encounterBean.getStatus());
            
            encounterString = encounterString.replace("${encounter.typeAlias}",encounterBean.getTypeAlias());
            encounterString = encounterString.replace("${encounter.typeName}",encounterBean.getTypeName());
            
            encounterString = encounterString.replace("${encounter.refPatient}",encounterBean.getRefPatient());
            encounterString = encounterString.replace("${encounter.encounterName}",encounterBean.getEncounterName());
            
            encounterString = encounterString.replace("${encounter.doctorAlias}",encounterBean.getDoctorAlias());
            encounterString = encounterString.replace("${encounter.doctorType}",encounterBean.getDoctorType());
            encounterString = encounterString.replace("${encounter.refDoctor}",encounterBean.getRefDoctor());
            encounterString = encounterString.replace("${encounter.doctorName}",encounterBean.getDoctorName());
            
            encounterString = encounterString.replace("${encounter.stime}",encounterBean.getStime());
            encounterString = encounterString.replace("${encounter.etime}",encounterBean.getEtime());
            
            encounterString = encounterString.replace("${encounter.reason}",encounterBean.getReason());
            
            encounterString = encounterString.replace("${encounter.refDiagnosis}",encounterBean.getRefDiagnosis());
            encounterString = encounterString.replace("${encounter.diagnosisCondition}",encounterBean.getDiagnosisCondition());
            encounterString = encounterString.replace("${encounter.diagnosisTypeAlias}",encounterBean.getDiagnosisTypeAlias());
            encounterString = encounterString.replace("${encounter.diagnosisTypeName}",encounterBean.getDiagnosisTypeName());
            encounterString = encounterString.replace("${encounter.diagnosisRank}",encounterBean.getDiagnosisRank());
            
            encounterString = encounterString.replace("${encounter.refOrigin}",encounterBean.getRefOrigin());
            encounterString = encounterString.replace("${encounter.originName}",encounterBean.getOriginName());
            encounterString = encounterString.replace("${encounter.originTypeAlias}",encounterBean.getOriginTypeAlias());
            encounterString = encounterString.replace("${encounter.originTypeName}",encounterBean.getOriginTypeName());
            encounterString = encounterString.replace("${encounter.destinationAlias}",encounterBean.getDestinationAlias());
            encounterString = encounterString.replace("${encounter.destinationName}",encounterBean.getDestinationName());
            
            encounterString = encounterString.replace("${encounter.refServiceProvider}",encounterBean.getRefServiceProvider());
            encounterString = encounterString.replace("${encounter.medicalInstitutionName}",encounterBean.getMedicalInstitutionName());
           
       
        
        return encounterString;
    }
    
     //检查申请xml生成
    public static String makeProcedureRequestString(ProcedureRequestBean procedureRequestBean, String path) {
        
        String  procedureRequestXml = getTemplateString(path);
           
        procedureRequestXml = procedureRequestXml.replace("${request.prId}",procedureRequestBean.getPrId());
        procedureRequestXml = procedureRequestXml.replace("${request.status}",procedureRequestBean.getStatus());
        procedureRequestXml = procedureRequestXml.replace("${request.intent}",procedureRequestBean.getIntent());
        procedureRequestXml = procedureRequestXml.replace("${request.inspectCode}",procedureRequestBean.getInspectCode());
        procedureRequestXml = procedureRequestXml.replace("${request.inspectName}",procedureRequestBean.getInspectName());

        procedureRequestXml = procedureRequestXml.replace("${request.refPatient}",procedureRequestBean.getRefPatient());           
        procedureRequestXml = procedureRequestXml.replace("${request.relationPatient}",procedureRequestBean.getRelationPatient());

        procedureRequestXml = procedureRequestXml.replace("${request.refEncounter}",procedureRequestBean.getRefencounter());  

        procedureRequestXml = procedureRequestXml.replace("${request.occurrenceDateTime}",procedureRequestBean.getOccurrenceDateTime());
        procedureRequestXml = procedureRequestXml.replace("${request.authoredOn}",procedureRequestBean.getAuthoredOn());

        procedureRequestXml = procedureRequestXml.replace("${request.refDoctor}",procedureRequestBean.getRefDcotor());
        procedureRequestXml = procedureRequestXml.replace("${request.applyDoctor}",procedureRequestBean.getApplyDoctor());

        procedureRequestXml = procedureRequestXml.replace("${request.reasonCode}",procedureRequestBean.getReasonCode());

        procedureRequestXml = procedureRequestXml.replace("${request.applyCode}",procedureRequestBean.getApplyCode());
        procedureRequestXml = procedureRequestXml.replace("${request.applyPosition}",procedureRequestBean.getApplyPosition());
                 
        
        return procedureRequestXml;
    }
    
     //检查报告xml生成
    public static String makeJcbgString(JcbgBean jcbgBean, String path) {
       
        String jcbgXml = getTemplateString(path);

        jcbgXml = jcbgXml.replace("${jcbg.id}", jcbgBean.getId());
        jcbgXml = jcbgXml.replace("${jcbg.lastUpdated}", jcbgBean.getLastUpdated());

        jcbgXml = jcbgXml.replace("${jcbg.documentAlias}", jcbgBean.getDocumentAlias());
        jcbgXml = jcbgXml.replace("${jcbg.documentAliasName}", jcbgBean.getDocumentAliasName());

        jcbgXml = jcbgXml.replace("${jcbg.type}", jcbgBean.getType());

        jcbgXml = jcbgXml.replace("${jcbg.compositionId}", jcbgBean.getCompositionId());
        jcbgXml = jcbgXml.replace("${jcbg.compositionLastUpdate}", jcbgBean.getCompositionLastUpdate());
        jcbgXml = jcbgXml.replace("${jcbg.compositionStatus}", jcbgBean.getCompositionStatus());
        jcbgXml = jcbgXml.replace("${jcbg.compositionTypeAlias}", jcbgBean.getCompositionTypeAlias());
        jcbgXml = jcbgXml.replace("${jcbg.compositionTypeName}", jcbgBean.getCompositionTypeName());

        jcbgXml = jcbgXml.replace("${jcbg.patientId}", jcbgBean.getPatientId());
        jcbgXml = jcbgXml.replace("${jcbg.refPatientDisplay}", jcbgBean.getRefPatientDisplay());

        jcbgXml = jcbgXml.replace("${jcbg.refEncounter}", jcbgBean.getRefEcounter());

        jcbgXml = jcbgXml.replace("${jcbg.reportDateTime}", jcbgBean.getReportDateTime());

        jcbgXml = jcbgXml.replace("${jcbg.refCompositionAuthor}", jcbgBean.getRefCompositionAuthor());
        jcbgXml = jcbgXml.replace("${jcbg.compositionAuthor}", jcbgBean.getCompositionAuthor());
        jcbgXml = jcbgXml.replace("${jcbg.compositionTitle}", jcbgBean.getCompositionTitle());
        jcbgXml = jcbgXml.replace("${jcbg.compositionLevel}", jcbgBean.getCompositionLevel());

        jcbgXml = jcbgXml.replace("${jcbg.patientActive}", jcbgBean.getPatientActive());
        jcbgXml = jcbgXml.replace("${jcbg.patientNameAll}", jcbgBean.getPatientNameAll());
        jcbgXml = jcbgXml.replace("${jcbg.patientFamilyName}", jcbgBean.getPatientFamilyName());
        jcbgXml = jcbgXml.replace("${jcbg.patientGivenName}", jcbgBean.getPatientGivenName());

        jcbgXml = jcbgXml.replace("${jcbg.patientPhoneType}", jcbgBean.getPatientPhoneType());
        jcbgXml = jcbgXml.replace("${jcbg.patientPhoneNumber}", jcbgBean.getPatientPhoneNumber());
        jcbgXml = jcbgXml.replace("${jcbg.patientPhoneUse}", jcbgBean.getPatientPhoneUse());

        jcbgXml = jcbgXml.replace("${jcbg.patientGender}", jcbgBean.getPatientGender());
        jcbgXml = jcbgXml.replace("${jcbg.patientBirthdate}", jcbgBean.getPatientBirthdate());
        jcbgXml = jcbgXml.replace("${jcbg.patientAddressType}", jcbgBean.getPatientAddressType());
        jcbgXml = jcbgXml.replace("${jcbg.patientAddressName}", jcbgBean.getPatientAddressName());


        jcbgXml = jcbgXml.replace("${jcbg.encounterId}", jcbgBean.getEncounterId());
        jcbgXml = jcbgXml.replace("${jcbg.encounterStatus}", jcbgBean.getEncounterStatus());
        jcbgXml = jcbgXml.replace("${jcbg.encounterTypeAlias}", jcbgBean.getEncounterTypeAlias());
        jcbgXml = jcbgXml.replace("${jcbg.encounterTypeName}", jcbgBean.getEncounterTypeName());
        jcbgXml = jcbgXml.replace("${jcbg.encounterTypeDetail}", jcbgBean.getEncounterTypeDetail());

        jcbgXml = jcbgXml.replace("${jcbg.encounterSDateTime}", jcbgBean.getEncounterSDateTime());
        jcbgXml = jcbgXml.replace("${jcbg.encounterEDateTime}", jcbgBean.getEncounterEDateTime());

        jcbgXml = jcbgXml.replace("${jcbg.observationId}", jcbgBean.getObservationId());
        jcbgXml = jcbgXml.replace("${jcbg.observationStatus}", jcbgBean.getObservationStatus());
        jcbgXml = jcbgXml.replace("${jcbg.observationTypeAlias}", jcbgBean.getObservationTypeAlias());
        jcbgXml = jcbgXml.replace("${jcbg.observationTypeName}", jcbgBean.getObservationTypeName());

        jcbgXml = jcbgXml.replace("${jcbg.observationItemAlias}", jcbgBean.getObservationItemAlias());
        jcbgXml = jcbgXml.replace("${jcbg.observationItemName}", jcbgBean.getObservationItemName());


        jcbgXml = jcbgXml.replace("${jcbg.observationDateTime}", jcbgBean.getObservationDateTime());

        jcbgXml = jcbgXml.replace("${jcbg.observationDoctorId}", jcbgBean.getObservationDoctorId());
        jcbgXml = jcbgXml.replace("${jcbg.observationDoctor}", jcbgBean.getObservationDoctor());

        jcbgXml = jcbgXml.replace("${jcbg.observationResult}", jcbgBean.getObservationResult());

        jcbgXml = jcbgXml.replace("${jcbg.observationPositionCode}", jcbgBean.getObservationPositionCode());
        jcbgXml = jcbgXml.replace("${jcbg.observationPosition}", jcbgBean.getObservationPosition());
        jcbgXml = jcbgXml.replace("${jcbg.observationMethodCode}", jcbgBean.getObservationMethodCode());
        jcbgXml = jcbgXml.replace("${jcbg.observationMethod}", jcbgBean.getObservationMethod());
        jcbgXml = jcbgXml.replace("${jcbg.observationEquipment}", jcbgBean.getObservationEquipment());
                 
      
        return jcbgXml;
    }
}
